package com.egreen.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.egreen.common.pojo.DTO.user.ChangeStateDTO;
import com.egreen.common.pojo.entity.user.User;
import com.egreen.common.pojo.entity.user.UserStates;
import com.egreen.user.exceptions.UserException;
import com.egreen.user.dao.UserStatesDao;
import com.egreen.user.service.IUserService;
import com.egreen.user.service.IUserStatesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLClientInfoException;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Service
public class UserStatesServiceImpl extends ServiceImpl<UserStatesDao, UserStates> implements IUserStatesService {
    @Autowired
    IUserService userService;

    @Autowired
    IUserStatesService userStatesService;


    @Override
    @Transactional(rollbackFor = {SQLClientInfoException.class, UserException.class})
    public void changeState(ChangeStateDTO changeStateDTO) {
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper.eq("id", changeStateDTO.getUserID());
        User user = userService.getOne(userWrapper);

        QueryWrapper<UserStates> statesWrapper = new QueryWrapper<>();
        statesWrapper.eq("description", changeStateDTO.getState());
        UserStates userStates = userStatesService.getOne(statesWrapper);

        user.setStates(userStates.getStates());
    }
}
